package person.wend.javalearnexample.util;

public class ObjectsExample {
    public static void main(String[] args) {
        equalsExample();
        hashExample();
        toStringExample();
        compareExample();
        nullMethodExample();
        checkIndexExample();
    }

    public static void equalsExample() {
        // 示例1 equals 比较两个对象是否相等
        System.out.println(java.util.Objects.equals(null, null)); // true
        System.out.println(java.util.Objects.equals(null, "Hello")); // false
        System.out.println(java.util.Objects.equals("Hello", null)); // false
        System.out.println(java.util.Objects.equals("Hello", "Hello")); // true
        // deepEquals
        System.out.println(java.util.Objects.deepEquals(new String[]{"Hello"}, new String[]{"Hello"})); // true
        System.out.println(java.util.Objects.deepEquals(new String[]{"Hello"}, new String[]{"World"})); // false
    }

    public static void hashExample() {
        // 示例2 hash
        System.out.println(java.util.Objects.hash("Hello", "World")); // 124732529
        System.out.println(java.util.Objects.hash("Hello", "World", "Java")); // 182916470

        //  hashCode
        System.out.println(java.util.Objects.hashCode("Hello")); // 69609650
    }

    public static void toStringExample() {
        // 示例3 toString
        System.out.println(java.util.Objects.toString(null)); // null
        System.out.println(java.util.Objects.toString("Hello")); // Hello
        System.out.println(java.util.Objects.toString("Hello", "World")); // Hello
        // toIdentityString
        System.out.println(java.util.Objects.toIdentityString("Hello")); // java.lang.String@1b6d3586
    }

    public static void compareExample() {
        // 示例5 compare
        System.out.println(java.util.Objects.compare("Hello", "World", String::compareTo)); // -15
        System.out.println(java.util.Objects.compare("World", "Hello", String::compareTo)); // 15
    }

    public static void nullMethodExample() {
        // 示例4 isNull
        System.out.println(java.util.Objects.isNull(null)); // true
        System.out.println(java.util.Objects.isNull("Hello")); // false
        // nonNull
        System.out.println(java.util.Objects.nonNull(null)); // false
        System.out.println(java.util.Objects.nonNull("Hello")); // true
        // requireNonNull
        java.util.Objects.requireNonNull("Hello", "Argument should not be null");
        // java.util.Objects.requireNonNull(null, "Argument should not be null"); // 抛出 NullPointerException
        // requireNonNullElse
        System.out.println(java.util.Objects.requireNonNullElse("Hello", "World")); // Hello
        System.out.println(java.util.Objects.requireNonNullElse(null, "World")); // World
        // requireNonNullElseGet
        System.out.println(java.util.Objects.requireNonNullElseGet("Hello", () -> "World")); // Hello
        System.out.println(java.util.Objects.requireNonNullElseGet(null, () -> "World")); // World

    }

    public static void checkIndexExample(){
        // checkIndex
        java.util.Objects.checkIndex(1, 3); // 不抛出异常
        // java.util.Objects.checkIndex(3, 3); // 抛出 IndexOutOfBoundsException
        // checkFromToIndex

        java.util.Objects.checkFromToIndex(1, 2, 3); // 不抛出异常
        // java.util.Objects.checkFromToIndex(1, 3, 2); // 抛出 IndexOutOfBoundsException
        // checkFromIndexSize
        java.util.Objects.checkFromIndexSize(1, 2, 3); // 不抛出异常
        // java.util.Objects.checkFromIndexSize(1, 3, 2); // 抛出 IndexOutOfBoundsException
    }
}
